package com.baoxin.chenxixc4coach.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baoxin.chenxixc4coach.GlideApp;
import com.baoxin.chenxixc4coach.R;
import com.baoxin.chenxixc4coach.bean.TestStudent;
import com.baoxin.chenxixc4coach.bean.TrainStudent;
import com.baoxin.chenxixc4coach.utils.MyLog;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

public class TestStudentAdapter extends RecyclerView.Adapter<TestStudentAdapter.ViewHolder> {
    private List<TestStudent> studentList;
    private Context context;
    private TestStuClickListener listener;

    public TestStudentAdapter(Context context, TestStuClickListener listener) {
        this.context = context;
        this.listener = listener;
    }

    public void setData(List<TestStudent> studentList) {
        this.studentList = studentList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.student_test_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        final TestStudent student = studentList.get(position);
        GlideApp.with(context)
                .load(student.getMap().get("tr_img"))
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .error(R.drawable.default_)
                .into(holder.icon);
        MyLog.d("AAA", student.getSerRes() + "");
        Integer res = student.getSerRes();
        if (res == null) {
            holder.res.setImageResource(R.drawable.test_res_set);
        } else if (res == 0) {
            holder.res.setImageResource(R.drawable.test_res_0);
        } else if (res == -1) {
            holder.res.setImageResource(R.drawable.test_res_f1);
        } else if (res == 1) {
            holder.res.setImageResource(R.drawable.test_res_1);
        }
        holder.name.setText(student.getMap().get("tr_name"));
        holder.id.setText(student.getMap().get("tr_idnumber"));
        holder.testTime.setText(student.getSerExamTime());
        holder.testPlace.setText(student.getSerFeild());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (student.getSerRes() == null) {
                    listener.showSetGradeDialog(studentList.get(holder.getAdapterPosition()));
                }

            }
        });
    }

    @Override
    public int getItemCount() {
        return studentList == null ? 0 : studentList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        View itemView;
        ImageView icon;
        ImageView res;
        TextView name;
        TextView id;
        TextView testTime;
        TextView testPlace;

        public ViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            icon = itemView.findViewById(R.id.student_icon);
            res = itemView.findViewById(R.id.res);
            name = itemView.findViewById(R.id.student_name);
            id = itemView.findViewById(R.id.student_id);
            testTime = itemView.findViewById(R.id.test_time);
            testPlace = itemView.findViewById(R.id.test_place);
        }
    }

    public interface TestStuClickListener {
        void showSetGradeDialog(TestStudent student);
    }
}
